package flc.ast.fragment;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.stark.calculator.databinding.FragmentTaxDeductionBinding;
import com.stark.calculator.tax.adapter.DeductionAdapter;
import com.stark.calculator.tax.model.DeductionBean;
import com.stark.calculator.tax.viewmodel.DeductionViewModel;

import java.util.ArrayList;
import java.util.List;

import flc.ast.R;
import flc.ast.adapter.MyDeductionAdapter;
import flc.ast.databinding.FragmentMyDeductionBinding;
import stark.common.basic.base.BaseFragment;
import stark.common.basic.event.EventStatProxy;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link MyDeductionFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class MyDeductionFragment extends BaseFragment<DeductionViewModel, FragmentMyDeductionBinding> {

    private MyDeductionAdapter mAdapter;

    @Override
    protected int onCreate() {
        return R.layout.fragment_my_deduction;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent5(getActivity(), mDataBinding.event5Container);
        LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
        mDataBinding.rvItem.setLayoutManager(layoutManager);
        mAdapter = new MyDeductionAdapter();
        mDataBinding.rvItem.setAdapter(mAdapter);
        mAdapter.setOnItemClickListener(this);
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        mAdapter.getItem(position).setChecked(!mAdapter.getItem(position).isChecked());
        mAdapter.notifyItemChanged(position);
    }

    @Override
    protected void initData() {
    }

    @Override
    protected DeductionViewModel initViewModel() {
        return new ViewModelProvider(this).get(DeductionViewModel.class);
    }

    @Override
    protected void initObserve() {
        super.initObserve();
        mViewModel.getDeductionList().observe(this, new Observer<List<DeductionBean>>() {
            @Override
            public void onChanged(List<DeductionBean> deductionBeans) {
                if (mAdapter != null) mAdapter.setNewInstance(deductionBeans);
            }
        });
        mViewModel.loadDeductions(getContext());
    }

    @Override
    protected void showError(Object obj) {
    }

    public ArrayList<DeductionBean> getDeductionBeanList() {
        if (mAdapter != null) {
            return (ArrayList<DeductionBean>) mAdapter.getData();
        }
        return null;
    }
}